"use strict";
const { Model } = require("sequelize");
const moment = require("moment");
moment.locale("zh-cn");
module.exports = (sequelize, DataTypes) => {
  class VisitLog extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  }
  VisitLog.init(
    {
      ip: DataTypes.STRING,
      path: DataTypes.STRING,
      level: DataTypes.STRING,
      message: DataTypes.STRING,
      meta: {
        type: DataTypes.STRING,
        get() {
          return JSON.parse(this.getDataValue("meta"));
        },
      },
      route: DataTypes.STRING,
      method: DataTypes.STRING,
      originName: DataTypes.STRING,
      user: DataTypes.STRING,
      query: DataTypes.TEXT,
      body: DataTypes.TEXT,
      userAgent: DataTypes.STRING,
      timestamp: {
        type: DataTypes.DATE,
        get() {
          return moment(this.getDataValue("timestamp")).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        },
      },
    },
    {
      sequelize,
      timestamps: false, // 没有 createdAt 与 updatedAt
      modelName: "VisitLog",
    }
  );
  return VisitLog;
};
